package com.sj.parksdk.fragment.profile;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.core.http.subscriber.TipProgressLoadingSubscriber;
import com.sj.parksdk.core.http.subscriber.TipRequestSubscriber;
import com.sj.parksdk.net.base.CommonResult;
import com.sj.parksdk.net.http.ApiHelper;
import com.sj.parksdk.net.manager.TokenManager;
import com.sj.parksdk.net.result.databean.Data_VertifyCode;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.utils.Utils;
import com.sj.parksdk.utils.XToastUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xui.utils.CountDownButtonHelper;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.edittext.materialedittext.MaterialEditText;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.common.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

@Page(name = "设置/修改登录密码 ")
public class FragmentUpatePasswordVertifyCode extends BaseFragment {
    @BindView(R2.id.tv_phone)
    TextView tvPhone;
    @BindView(R2.id.et_verify_code)
    MaterialEditText etVerifyCode;
    @BindView(R2.id.btn_get_verify_code)
    RoundButton btnGetVerifyCode;
    @BindView(R2.id.btn_next)
    SuperButton btnNext;

    private CountDownButtonHelper mCountDownHelper;
    private String phone;

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_update_password_vertifcode;
    }

    @Override
    protected void initViews() {
        mCountDownHelper = new CountDownButtonHelper(btnGetVerifyCode, 60);
        mCountDownHelper.setOnCountDownListener(new CountDownButtonHelper.OnCountDownListener() {
            @Override
            public void onCountDown(int time) {
                btnGetVerifyCode.setText(ResUtils.getString(R.string.xui_count_down_finish)+time+"s");
            }

            @Override
            public void onFinished() {
                btnGetVerifyCode.setText(ResUtils.getString(R.string.xui_count_down_finish));
            }
        });
    }

    @Override
    protected void initData() {
        phone = TokenManager.getInstance().getLoginUser().getPhone();
        tvPhone.setText("手机号  " + (StringUtils.isEmpty(phone)?"号码获取失败,请重新登录": Utils.encryptionStr(phone,4)));
    }

    @SingleClick
    @OnClick({R2.id.btn_get_verify_code, R2.id.btn_next})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_get_verify_code) {
            getVerifyCode();
        } else if (id == R.id.btn_next) {
            if (etVerifyCode.validate()) {
                verifyCode();
            }
        }
    }

    @SuppressLint("CheckResult")
    private void verifyCode() {
        CustomRequest request = XHttp.custom();
        request.apiCall(ApiHelper.getInstance(request)
                .vertifyCode(phone, etVerifyCode.getEditValue()))
                .subscribeWith(new TipProgressLoadingSubscriber<Data_VertifyCode>() {
                    @Override
                    protected void onSuccess(Data_VertifyCode data) {
                        gotoUpdate(data.getToken());
                    }
                });
    }

    private void gotoUpdate(String s) {
//        mCountDownHelper.cancel();
        mCountDownHelper.recycle();
        PageOption.to(FragmentUpdatePassword.class)
                .putString(Extras.BASE_CODE, s)
                .setAddToBackStack(false)
                .open(this);
    }

    @SuppressLint("CheckResult")
    private void getVerifyCode() {
        if (!StringUtils.isEmpty(phone)) {
            mCountDownHelper.start();
            CustomRequest request = XHttp.custom();
            request.call(ApiHelper.getInstance(request)
                    .loginSendMesg(phone, false))
                    .subscribeWith(new TipRequestSubscriber<CommonResult>() {
                        @Override
                        protected void onSuccess(CommonResult result) {
                            if (result.getCode() != 200) XToastUtils.error(result.getMessage());
                        }
                    });
        }
    }
}
